package com.cgc.shop.task;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.springframework.stereotype.Component;

import com.cgc.shop.model.Category;
import com.cgc.shop.model.Product;
import com.cgc.shop.service.CategoryService;
import com.cgc.shop.service.ProductService;

/**
 * 定时获取热门类别中的推荐商品类
 * @author Administrator
 *
 */
@Component
public class CommendProductTask extends TimerTask {
	
	private ServletContext applicationContext;
	
	@Resource
	private ProductService productService;
	
	@Resource
	private CategoryService categoryService;

	public ServletContext getApplicationContext() {
		return applicationContext;
	}

	public void setApplicationContext(ServletContext applicationContext) {
		this.applicationContext = applicationContext;
	}

	@Override
	public void run() {
		System.out.println("CommendProductTask" + "------run()------");
		List<List<Product>> categoryList  = new ArrayList<List<Product>>();
		//定时获取热门类别中的推荐商品放在application属性中供首页页面使用
		for (Category c : categoryService.queryByPopular(true)) {
			List<Product> productList = productService.queryByCategoryId(c.getId());
			categoryList.add(productList);
		}
		applicationContext.setAttribute("categoryList", categoryList);
	}
}
